import unittest

from ula import *
import base

class Testula(unittest.TestCase):
    
    def setUp(self):
        self.ula = ULA()

    def test_ula(self):
        self.assertEqual(self.ula.operando1,'0'*base.TAM)
        self.assertEqual(self.ula.operando2,'0'*base.TAM)
        
        #teste soma positivo com positivo
        a ='0'*(base.TAM-1)+'1'
        b ='0'*(base.TAM-1)+'1'
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.soma(),'0'*(base.TAM-2)+'1'+'0')
        self.assertEqual(self.ula.carry,'0')
        self.assertEqual(self.ula.overflow,'0')
        self.assertEqual(self.ula.sinal,'0')
        self.assertEqual(self.ula.zero,'0')
        
        #teste soma positivo com negativo
        a ='1'*(base.TAM)
        b ='0'*(base.TAM-3)+'100'
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.soma(),'0'*(base.TAM-2)+'11')
        self.assertEqual(self.ula.carry,'1')
        self.assertEqual(self.ula.overflow,'0')
        self.assertEqual(self.ula.sinal,'0')
        self.assertEqual(self.ula.zero,'0')
        
        #teste soma negativo com negativo
        a ='1'*(base.TAM)
        b ='1'*(base.TAM-1)+'0'
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.soma(),'1'*(base.TAM-2)+'01')
        self.assertEqual(self.ula.carry,'1')
        self.assertEqual(self.ula.overflow,'0')
        self.assertEqual(self.ula.sinal,'1')
        self.assertEqual(self.ula.zero,'0')
        
        #teste soma = 0
        a ='0'*(base.TAM)
        b ='0'*(base.TAM)
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.soma(),'0'*(base.TAM))
        self.assertEqual(self.ula.carry,'0')
        self.assertEqual(self.ula.overflow,'0')
        self.assertEqual(self.ula.sinal,'0')
        self.assertEqual(self.ula.zero,'1')
        
        #teste soma com overflow
        a ='1'+'0'*(base.TAM-2)+'1'
        b ='1'+'0'*(base.TAM-2)+'1'
        self.ula.setOperandos(a,b)
        self.ula.soma()
        self.assertEqual(self.ula.carry,'1')
        self.assertEqual(self.ula.overflow,'1')
        self.assertEqual(self.ula.sinal,'0')
        self.assertEqual(self.ula.zero,'0')
        
        #teste de NOT 1 operando
        a = '0'+(base.TAM-2)*'1'+'0'
        self.ula.setOperandos(a,'0'*(base.TAM))
        self.assertEqual(self.ula.notOperando1(), '1'+(base.TAM-2)*'0'+'1')
        
        #teste de NOT 2 operando
        b = '0'+(base.TAM-2)*'1'+'0'
        self.ula.setOperandos('0'*(base.TAM),b)
        self.assertEqual(self.ula.notOperando2(), '1'+(base.TAM-2)*'0'+'1')
        
        #teste de complemento a 2 operando1
        a = (base.TAM-1)*'0'+'1'
        self.ula.setOperandos(a,base.TAM*'0')
        self.assertEqual(self.ula.complementoA2Operando1(), '1'*base.TAM)
        
        #teste de complemento a 2 operando2
        b = (base.TAM-1)*'0'+'1'
        self.ula.setOperandos('0'*base.TAM,b)
        self.assertEqual(self.ula.complementoA2Operando2(), '1'*base.TAM)
        
        #teste soma de um numero com o seu complemento
        b = (base.TAM-1)*'0'+'1'
        self.ula.setOperandos('0'*base.TAM,b)
        c =self.ula.complementoA2Operando2()
        self.ula.setOperandos(c,b)
        self.assertEqual(self.ula.soma(),'0'*base.TAM)
        
        #teste and logico
        b = base.TAM*'0'
        a = base.TAM*'1' 
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.andLogico(),'0'*base.TAM)
        
         #teste and logico2
        b = base.TAM*'1'
        a = base.TAM*'1' 
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.andLogico(),'1'*base.TAM)
        
         #teste and logico
        b = 10*'0'+10*'1'+12*'0'
        a = 10*'1'+10*'0'+12*'1'
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.andLogico(),'0'*base.TAM)
        
         #teste and logico
        b = base.TAM*'0'
        a = base.TAM*'1' 
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.orLogico(),'1'*base.TAM)
        
         #teste and logico2
        b = base.TAM*'1'
        a = base.TAM*'1' 
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.orLogico(),'1'*base.TAM)
        
         #teste and logico
        b = 10*'0'+10*'1'+12*'0'
        a = 10*'1'+10*'0'+12*'1'
        self.ula.setOperandos(a,b)
        self.assertEqual(self.ula.orLogico(),'1'*base.TAM)
        
        #testes de copiar a entrada 1
        a ='1'*base.TAM
        self.ula.setOperandos(a,base.TAM*'0')
        self.assertEqual(self.ula.copiaEntradaOperando1(),a)
        
        #testes de copiar a entrada 2
        b='1'*base.TAM
        self.ula.setOperandos(base.TAM*'0',b)
        self.assertEqual(self.ula.copiaEntradaOperando2(),b)
        
        #teste do clear
        a='1'*base.TAM
        self.ula.setOperandos(a,a)
        self.assertEqual(self.ula.clearOperando(),base.TAM*'0')
        
        #teste do shift left
        a='1'*base.TAM
        self.ula.setOperandos(a,a)
        self.assertEqual(self.ula.shiftLeft(),(base.TAM-1)*'1'+'0')
        
        #teste do shift right
        a='1'*base.TAM
        self.ula.setOperandos(a,a)
        self.assertEqual(self.ula.shiftRight(),'0'+(base.TAM-1)*'1')        
        
        
if __name__ == '__main__':
    unittest.main()